<template>
	<view>
		<cu-custom bgColor="bg-blue" :isBack="true"><block slot="content">注册授权</block></cu-custom>
		<view class="flex justify-center margin-top-xxl">
			<image src="../../../static/img/logo.png" class="logo  margin-top-xxl"></image>
		</view>
		
		<view class="mine-auth-comment flex justify-center margin-top-xxl">
			<button v-if="!checkSession||!member" :disabled="flag=='N'" class="cu-btn bg-green margin-top-xl lg" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true">授权登录</button>
			<button v-else-if="!member.phonenumber" :disabled="flag=='N'" class="cu-btn bg-green margin-top-xl lg" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"  withCredentials="true">绑定手机</button>
			<button type="primary" class="cu-btn bg-grey margin-left margin-top-xl lg" @click="back" >取  消</button>
		</view>
		<view class="margin-top">
			<checkbox-group @change="checkChange" id="packingIntact" class="basis-sm text-center flex justify-center border padding">
				<label >
					<checkbox value="Y" style="transform: scale(0.6)" />
					同意<text @click="goFwxy" class="text-blue">《猎阵生物服务协议》</text>和<text @click="goYszc" class="text-blue">《隐私政策》</text>
				</label >
			</checkbox-group>
		</view>
		<view class="margin-top  text-center">
				仅限使用猎阵生物检测登记和查询的用户
		</view>
	</view>
</template>

<script>
	export default {
		name: "auth",
		data() {
			return {
				checkSession:false,
				member:uni.getStorageSync("member"),
				code:"",
				openid:"",
				sessionKey:"",
				signature:"",
				rawData:{},
				flag:"N",
				authData:{
					userId:"",
					openId:"",
					nickName:"",
					phonenumber:'',
					userName:'',
					userType:'90',
					sex:1,
					avatar:""
				 },
				numList: [{
						name: '开始'
					}, {
						name: '等待'
					}, {
						name: '错误'
					}, {
						name: '完成'
					}],
				num: 0,
				
			}
		},
		onLoad: function (option) {
			let self=this
			self.getCheckSession()
			
		},
		methods: {
				back(){
					uni.navigateBack({
						delta: 1
					});
				},
				goFwxy(){
					uni.navigateTo({
						url:"/page/common/service/service"
					})
				},
				goYszc(){
					uni.navigateTo({
						url:"/pages/common/privacy/privacy"
					})
				},
				NumSteps() {
					this.num= this.num == this.numList.length - 1 ? 0 : this.num + 1				
				},
				checkChange(e){
					console.log(JSON.stringify(e))
					if(e.detail.value.length>0){
						this.flag="Y"
					}else{
						this.flag="N"
					}
				},
				getCheckSession(){
					let self=this
					uni.checkSession({
						success: function (res) {
							console.log("======存在登陆态=======")
						      //存在登陆态
							  self.checkSession=true
						},
						fail: function () {
						  //不存在登陆态
							console.log("======不存在登陆态=======")
							  self.checkSession=false
						}
					})
				},
				getPhoneNumber: function(e) {
					var that = this
					if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
						 uni.showToast({
							title:"绑定手机失败",
							duration:2000
						 })
					} else {
						uni.login({
							provider: 'weixin',
							success: function(res) {
							
								let data= {
									'encryptedData': e.detail.encryptedData,
									'iv': e.detail.iv,
									'code': that.code,
									sessionKey:that.sessionKey,
									signature:that.signature,
									rawData:that.rawData,
								}
								 
								that.bblosApi.memberApi.getWxPhone(that.bblos.appid,data).then(res => {
									 let phone=res.data.phoneNumber
									 if(!phone||phone==undefined||phone=="undefined"){
										uni.showToast({
											title:"失败请重试",
											duration:2000,
											success() {
											}
										})
										 return;
									 }
									that.authData.openId=that.openid
									that.authData.phonenumber=phone
									that.authData.userName=phone
									that.authData.email=phone+"@qq.com"
									that.authData.userId=that.member.userId
									that.bblosApi.memberApi.updateMember(that.authData).then(res => {
										if(res.data.code==200){
											that.member.phonenumber=phone
											uni.setStorageSync("member",that.member)
											uni.showToast({
												title:"绑定手机成功",
												duration:2000,
												success() {
													setTimeout(function(){
														uni.navigateTo({
															url:"/pages/index/index"
														})
													},2000)
												}
											})
										}else{
											uni.showToast({
												title:"绑定手机失败",
												duration:2000,
												success() {
													setTimeout(function(){
														uni.navigateBack({
															delta: 1
														})
													},2000)
												}
											})
											
										}
										
									 });
								 });
							}
						})
					}
				},
				getUserInfo: function(loginType, cb) {					
					var that = this
						//1.调用登录接口
					uni.login({
						provider: 'weixin',
						success: function(res) {
							that.code=res.code
							uni.getUserInfo({
								provider: 'weixin',
								success: function(res) {
									that.getCheckSession()
									that.signature=res.signature
									that.rawData=res.rawData
									that.authData.nickName=res.userInfo.nickName
									that.authData.sex=res.userInfo.gender
									that.authData.avatar=res.userInfo.avatarUrl
									
									that.bblosApi.memberApi.getWxOpenId(that.bblos.appid,{code:that.code}).then(res => {
											
										that.openid=res.data.openid
										that.sessionKey=res.data.sessionKey
										that.authData.openId=that.openid
										that.authData.userName=that.openid
										that.bblosApi.memberApi.loginByWx(that.authData).then(res => {
											if(res.data.code==200){
												uni.setStorageSync("token",res.data.token)
												that.bblosApi.memberApi.getMemberInfo().then(res => {
													if(res.data.code==200){
														uni.showToast({
															title:"授权成功"
														})
														uni.setStorageSync("member",res.data.user)
														uni.setStorageSync("roles",res.data.roles)
														uni.setStorageSync("permissions",res.data.permissions)
														that.member=res.data.user
														if(res.data.user.phonenumber!=''){
															setTimeout(function(){
																uni.navigateTo({
																	url:"/pages/index/index"
																})
															},2000)
															
														}
													}
												});
												
											}else{
												uni.showToast({
													title:"授权出错"
												})
											}
										});
									})
								},
								fail: function(e) {
									console.log(e)
									uni.showToast({
										title:"拒绝授权"
									})
								},
							})
							
							
						}
					})
				}
		}
	}
</script>

<style>
	.mine-auth-comment{
		
	}
	.logo{
		width: 300upx;
		height: 150upx;
	}
	.cu-card.case.no-card .image {
		margin: 0;
		overflow: hidden;
		border-radius: 0;
	}
</style>
